package me.lc.linklink.layer;

import me.lc.linklink.core.GameManager;
import me.lc.linklink.core.GameMap;
import me.lc.linklink.core.GameSystem;
import me.lc.linklink.core.GameType;
import me.lc.linklink.core.StageManager;
import me.lc.linklink.entity.StageInfo;
import me.lc.linklink.sprite.ComboBlock;
import me.lc.linklink.sprite.LoseShowBlock;
import me.lc.linklink.sprite.PauseShopBlock;
import me.lc.linklink.sprite.ProcessBlock;
import me.lc.linklink.sprite.ScoreToolsBlock;
import me.lc.linklink.sprite.WinShowBlock;
import me.lc.linklink.util.AudioHelper;
import android.view.MotionEvent;

import com.wiyun.engine.actions.Action.Callback;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.ScaleTo;
import com.wiyun.engine.actions.Sequence;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.utils.ResolutionIndependent;

public class GameLayer extends Layer {

	private ProcessBlock processBlock;
	private ScoreToolsBlock scoreToolsBlock;
	private ComboBlock comboBlock;
	private LoseShowBlock loseBlock;
	private WinShowBlock winBlock;
	private PauseShopBlock pauseBlock;
	private Sprite readyGo;

	private Sprite gameUp;
	private Sprite gameDown;
	private Sprite gameLeft;
	private Sprite gameRight;
	private Sprite gameNormal;
	private Sprite gameUpDown;
	private Sprite gameLeftRight;

	private void initGameTypeNotify() {
		gameUp = Sprite.make(GameSystem.gameUpTex);
		gameUp.setAlpha(220);
		gameUp.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);

		gameDown = Sprite.make(GameSystem.gameDownTex);
		gameDown.setAlpha(220);
		gameDown.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);

		gameLeft = Sprite.make(GameSystem.gameLeftTex);
		gameLeft.setAlpha(220);
		gameLeft.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);

		gameRight = Sprite.make(GameSystem.gameRightTex);
		gameRight.setAlpha(220);
		gameRight.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);

		gameNormal = Sprite.make(GameSystem.gameNormalTex);
		gameNormal.setAlpha(220);
		gameNormal.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);

		gameLeftRight = Sprite.make(GameSystem.gameLeftRightTex);
		gameLeftRight.setAlpha(220);
		gameLeftRight.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);

		gameUpDown = Sprite.make(GameSystem.gameUpDownTex);
		gameUpDown.setAlpha(220);
		gameUpDown.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);
	}

	private void showGameTypeNotify() {
		if (GameManager.instance.CurrentGameType == GameType.Normal
				&& StageManager.instance.currentStage >= 0
				&& StageManager.instance.currentStage < StageManager.instance.stageInfos
						.size()) {
			StageInfo info = StageManager.instance.stageInfos
					.get(StageManager.instance.currentStage);
			switch (info.getArrType()) {
			case Top:
				gameUp.setVisible(true);
				addChild(gameUp);
				break;
			case Bottom:
				gameDown.setVisible(true);
				addChild(gameDown);
				break;
			case Left:
				gameLeft.setVisible(true);
				addChild(gameLeft);
				break;
			case Right:
				gameRight.setVisible(true);
				addChild(gameRight);
				break;
			case Normal:
				gameNormal.setVisible(true);
				addChild(gameNormal);
				break;
			case LeftRight:
				gameLeftRight.setVisible(true);
				addChild(gameLeftRight);
				break;
			case TopBottom:
				gameUpDown.setVisible(true);
				addChild(gameUpDown);
				break;
			}
		} else if (GameManager.instance.CurrentGameType == GameType.Time) {
			// 争分夺秒暂时只有普通模式
			gameNormal.setVisible(true);
			addChild(gameNormal);
		}
	}

	private void hideGameType() {
		gameUp.setVisible(false);
		gameDown.setVisible(false);
		gameRight.setVisible(false);
		gameLeft.setVisible(false);
		gameNormal.setVisible(false);
		gameLeftRight.setVisible(false);
		gameUpDown.setVisible(false);
	}

	public GameLayer() {
		scoreToolsBlock = new ScoreToolsBlock();
		processBlock = new ProcessBlock();
		comboBlock = new ComboBlock();
		loseBlock = new LoseShowBlock();
		loseBlock.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);
		winBlock = new WinShowBlock();
		winBlock.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);
		pauseBlock = new PauseShopBlock();
		pauseBlock.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);
		initGameTypeNotify();
	}

	/**
	 * 初始化关卡游戏
	 * 
	 * @param stageIndex
	 */
	public void initGameLayer(int stageIndex) {

		removeAllChildren(true);
		addChild(scoreToolsBlock);
		addChild(processBlock);

		// 生成游戏地图
		GameMap.gen(this, stageIndex);
		addChild(comboBlock);

		readyGo = Sprite.make(GameSystem.readyGoTex);
		// 设置到中心位置
		float readyGoY = GameSystem.Screen_Height * 0.675f;
		float readyGoX = GameSystem.Screen_Width / 2.0f;
		readyGo.setVisible(false);
		readyGo.setPosition(readyGoX, readyGoY);
		addChild(readyGo);
		// 游戏控制
		GameManager.instance.setComboBlock(comboBlock);
		GameManager.instance.initNewGame(processBlock, stageIndex, this,
				GameType.Normal);

		// ready go动画
		IntervalAction a = (IntervalAction) ScaleTo.make(1.3f, 0.8f, 1.1f)
				.autoRelease();
		IntervalAction r = (IntervalAction) ScaleTo.make(0.2f, 1.1f, 0.6f)
				.autoRelease();
		IntervalAction seq = (IntervalAction) Sequence.make(a, r).autoRelease();
		seq.setCallback(new ReadyGoAnimationCallback());
		readyGo.runAction(seq);
	}

	public void forceComboBlock() {
		removeChild(comboBlock, true);
		addChild(comboBlock);
	}

	/**
	 * 开始争分夺秒游戏
	 */
	public void initTimeGame() {

		// 生成游戏地图
		removeAllChildren(true);
		addChild(scoreToolsBlock);
		addChild(processBlock);

		// 生成游戏地图
		GameMap.gen(this, GameSystem.Time_Stage_Index);
		addChild(comboBlock);

		readyGo = Sprite.make(GameSystem.readyGoTex);
		// 设置到中心位置
		float readyGoY = GameSystem.Screen_Height * 0.675f;
		float readyGoX = GameSystem.Screen_Width / 2.0f;
		readyGo.setVisible(false);
		readyGo.setPosition(readyGoX, readyGoY);
		addChild(readyGo);
		// 游戏控制
		GameManager.instance.setComboBlock(comboBlock);
		GameManager.instance.initNewGame(processBlock,
				GameSystem.Time_Stage_Index, this, GameType.Time);

		// ready go动画
		IntervalAction a = (IntervalAction) ScaleTo.make(1.3f, 0.8f, 1.1f)
				.autoRelease();
		IntervalAction r = (IntervalAction) ScaleTo.make(0.2f, 1.1f, 0.6f)
				.autoRelease();
		IntervalAction seq = (IntervalAction) Sequence.make(a, r).autoRelease();
		seq.setCallback(new ReadyGoAnimationCallback());
		readyGo.runAction(seq);
	}

	@Override
	public boolean wyTouchesBegan(MotionEvent event) {
		if (!GameManager.instance.isPaused()) {
			// 转换坐标系
			WYPoint point = this.convertTouchToNodeSpace(event);
			if (point.y <= ResolutionIndependent
					.resolveDp(GameSystem.BLOCK_SIZE)) {
				// 道具处理
				scoreToolsBlock.processTouch(point);
			} else {
				GameMap.processTouch(point);
			}
			return true;
		}
		return true;
	}

	public void showLose() {
		addChild(loseBlock);
		IntervalAction showAnimation = (IntervalAction) ScaleTo.make(0.5f,
				0.5f, 1.0f).autoRelease();
		loseBlock.runAction(showAnimation);
	}

	public void showWin() {
		addChild(winBlock);
		IntervalAction showAnimation = (IntervalAction) ScaleTo.make(0.5f,
				0.5f, 1.0f).autoRelease();
		winBlock.runAction(showAnimation);
		winBlock.showScore();
	}

	class ReadyGoAnimationCallback implements Callback {

		@Override
		public void onStart(int arg0) {
			readyGo.setVisible(true);
			AudioHelper.readyGo();
			showGameTypeNotify();
		}

		@Override
		public void onStop(int arg0) {
			readyGo.setVisible(false);
			removeChild(readyGo, true);
			// 开始游戏
			GameManager.instance.startGame();
			hideGameType();
		}

		@Override
		public void onUpdate(int arg0, float arg1) {

		}
	}

	public void pauseGame() {
		if (!GameManager.instance.isPaused()) {
			GameManager.instance.pauseGame();
			addChild(pauseBlock);
			IntervalAction showAnimation = (IntervalAction) ScaleTo.make(0.3f,
					0.5f, 1.0f).autoRelease();
			pauseBlock.runAction(showAnimation);
			pauseBlock.show();
		}
	}

	public void resumeGame() {
		if (GameManager.instance.isPaused()) {
			GameManager.instance.resumeGame();
			removeChild(pauseBlock, true);
		}
	}

	public void updateGold() {
		if (GameManager.instance.isPaused()) {
			Director.getInstance().runOnGLThread(new Runnable() {

				@Override
				public void run() {
					pauseBlock.updateGold();
				}
			});

		}

	}

	public void updateTool() {
		scoreToolsBlock.updateToolsCount();
	}

}
